package com.bupt.godxian.charging.controller;

import com.bupt.godxian.charging.config.CommonResponse;
import com.bupt.godxian.charging.entity.User;
import com.bupt.godxian.charging.service.UserService;
import com.bupt.godxian.charging.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class AuthController {
    //JwtUtil 类是一个自定义的 JWT 工具类，它负责生成和验证 JWT。
    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    //在使用MyBatis-Plus时，可以直接使用实体类作为登录请求的对象。
    //ResponseEntity<?>是Spring框架提供的一个用于包装HTTP响应的类。它封装了响应的状态码、响应头、响应体等信息
    public ResponseEntity<Map<String, Object>> login(@RequestBody User loginRequest) {
        //如果与数据库绑定的实体类已经包含了请求的字段则可以世界使用实体类
        // 假设这里进行用户名和密码的验证，验证通过后生成JWT并返回给客户端
        String username = loginRequest.getUsername();
        String password = loginRequest.getPassword();

        //查找数据库中是否存在该用户名的用户对象，然后匹配密码
        User loginUser = userService.findByUsername(username);
        if (loginUser == null || !loginUser.getPassword().equals(password)) {
            // 用户验证失败，返回错误响应
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", -1);
            errorResponse.put("message", "Invalid username or password");
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(errorResponse);
        }
        //根据用户名生成的
        String token = jwtUtil.generateToken(username);

        Map<String, Object> responseData = new HashMap<>();
        responseData.put("token", token);
        //获取用户身份
//        Integer admin=userService.AdminById();
//        loginRequest.setAdmin();
        responseData.put("isAdmin", loginUser.isAdmin());

        Map<String, Object> responseBody = new HashMap<>();
        responseBody.put("code", 0);
        responseBody.put("message", "Success");
        responseBody.put("data", responseData);

//        LoginResponseData responseData = new LoginResponseData();
//        responseData.setToken(token);
//        responseData.setAdmin(loginUser.isAdmin());
//
//        CommonResponse response = new CommonResponse();
//        response.setCode(0);
//        response.setMessage("Success");
//        response.setData(responseData);

        return ResponseEntity.ok(responseBody);

    }

    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> register(@RequestBody Map<String, String> registerRequest) {
        String username = registerRequest.get("username");
        String password = registerRequest.get("password");
        String rePassword =registerRequest.get("re_password");

        if (!password.equals(rePassword)) {
            Map<String, Object> responseBody = new HashMap<>();
            responseBody.put("code", -1);
            responseBody.put("message", "Password not match");
            return ResponseEntity.badRequest().body(responseBody);
        }

        User existingUser = userService.findByUsername(username);
        if (existingUser != null) {
            Map<String, Object> responseBody = new HashMap<>();
            responseBody.put("code", -1);
            responseBody.put("message", "Username already exists");
            return ResponseEntity.badRequest().body(responseBody);
        }

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        //如果用户名中存在admin则认为是管理员
        if (password.contains("admin")){
            user.setAdmin(1);
        }else {
            user.setAdmin(0);
        }

        userService.insert(user);

        Map<String, Object> responseBody = new HashMap<>();
        responseBody.put("code", 0);
        responseBody.put("message", "Success");
        return ResponseEntity.ok(responseBody);
    }

}







